﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Windows.ApplicationModel.Core;
using Windows.UI;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Xaml.Shapes;
using com.IronOne.BoardPACWinApp.Common;
using com.IronOne.BoardPACWinApp.Helpers;
using com.IronOne.BoardPACWinApp.View.UserControl;
using com.IronOne.BoardPACWinAppBO.Category;
using com.IronOne.BoardPACWinAppBO.General;
using com.IronOne.BoardPACWinAppBO.Meeting;
using com.IronOne.BoardPACWinAppBO.Sync;
using com.IronOne.BoardPACWinAppService.Auth;
using com.IronOne.BoardPACWinAppService.Category;
using com.IronOne.BoardPACWinAppService.Meeting;
using com.IronOne.BoardPACWinAppUtil.Util;
using GalaSoft.MvvmLight.Messaging;

// The Basic Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234237

namespace com.IronOne.BoardPACWinApp.View.Page
{
    /// <summary>
    ///     A basic page that provides characteristics common to most applications.
    /// </summary>
    public sealed partial class CalenderPage
    {
        public static CategoryModel GlobalCategorySelectedItem;
        public static SubCategoryModel GlobalSubCategorySelectedItem;
        public static int GlobalSelectedCalender = -1;
        private ICategoryService _categoryService;
        private IMeetingService _meetingService;
        private INavigationService _navigationService;
        private int _globalCategoryId = -1;
        private List<CategoryModel> _globalCategoryList;
        private List<MeetingModel> _globalMeetings;
        private int _globalSubCategoryId = -1;
        private List<SubCategoryModel> _globalSubCategoryList;
        private bool _isInitialPageLoad1 = true;
        private readonly IAuthService _authService;
        private readonly List<Grid> _calenderGrids = new List<Grid>();
        private readonly List<Grid> _calenderGridWrappers = new List<Grid>();
        private readonly List<TextBlock> _currentDateTexts = new List<TextBlock>();

        public CalenderPage()
        {
            InitializeComponent();
            NavigationHelper = new NavigationHelper(this);
            _navigationService = new NavigationService();
            _categoryService = new CategoryService();
            _meetingService = new MeetingService();
            _authService = new AuthService();
        }

        //public DateTime CurrentDate { get; set; }
        public DateTime SelectedDate { get; set; }

        /// <summary>
        ///     NavigationHelper is used on each page to aid in navigation and
        ///     process lifetime management
        /// </summary>
        public NavigationHelper NavigationHelper { get; private set; }

        protected override async void HandleMessages(NotificationMessage<MessageDataModel> mdm)
        {
            try
            {
                base.HandleMessages(mdm);
                await BasePage.GetInstance().HandleMessages(mdm);

                if (mdm.Notification.Equals(MessageCodes.SYNC_UPDATED_DATA))
                {
                    var sudm = (SyncUpdatedDataModel) mdm.Content.MessageData;
                    if (sudm.IsNewOrUpdatedCategories)
                    {
                        await PopulateCategoryListView();
                        return;
                    }

                    if (sudm.IsNewOrUpdatedSubCategories)
                    {
                        #region GetSubCategoriesByCategoryId

                        var lhdm =
                            await
                                CommonHelper.GetInstance()
                                    .RefereshSubCategoryListView(_categoryService, _globalCategoryId, _globalSubCategoryList,
                                        SubCategoryListView, GlobalSubCategorySelectedItem);
                        _globalSubCategoryList = lhdm.GlobalSubCategoryList;

                        #endregion
                    }
                }
            }
            catch (Exception)
            {
            }
        }

        
        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            NavigationHelper.OnNavigatedTo(e);

            await PopulateCategoryListView();
            //await InitializeCalendar();
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            NavigationHelper.OnNavigatedFrom(e);
        }

        private async Task PopulateCategoryListView()
        {
            try
            {
                var helper =
                    await
                        CommonHelper.GetInstance()
                            .PopulateCategoryListView(_globalCategoryList, _categoryService, GlobalCategorySelectedItem,
                                CategoryListView, _globalSubCategoryId, GlobalSubCategorySelectedItem);

                _globalCategoryList = helper.GlobalCategoryList;
                GlobalCategorySelectedItem = helper.GlobalCategorySelectedItem;
                _globalSubCategoryId = helper.GlobalSubCategoryId;
                GlobalSubCategorySelectedItem = helper.GlobalSubCategorySelectedItem;
            }
            catch (Exception)
            {
            }
        }

        private async void CategoryListViewSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                #region CommonCategoryListViewSelectionChanged

                var lhdm =
                    await
                        CommonHelper.GetInstance()
                            .CommonCategoryListViewSelectionChanged(CategoryListView, _categoryService,
                                SubCategoryListView, GlobalSubCategorySelectedItem);
                if (lhdm != null)
                {
                    GlobalCategorySelectedItem = lhdm.GlobalCategorySelectedItem;
                    _globalCategoryId = lhdm.GlobalCategoryId;
                    _globalSubCategoryList = lhdm.GlobalSubCategoryList;
                    GlobalSubCategorySelectedItem = lhdm.GlobalSubCategorySelectedItem;
                }

                #endregion

                if (!_isInitialPageLoad1)
                    GlobalSelectedCalender = -1; //NOTE: To select the current month everytime selection changes
                await InitializeCalendar();

                _isInitialPageLoad1 = false;
            }
            catch (Exception)
            {
            }
        }

        private async void SubCategoryListViewSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                #region SubCategoryListViewSelectionChanged

                var lhdm = CommonHelper.GetInstance().CommonSubCategoryListViewSelectionChanged(SubCategoryListView);
                _globalSubCategoryId = lhdm.GlobalSubCategoryId;
                GlobalSubCategorySelectedItem = lhdm.GlobalSubCategorySelectedItem;

                #endregion

                if (_globalSubCategoryId == -1)
                    return; //No need to load the meetings again, category selecion changed already did it.

                if (!_isInitialPageLoad1)
                    GlobalSelectedCalender = -1; //NOTE: To select the current month everytime selection changes
                await InitializeCalendar();
            }
            catch (Exception)
            {
            }
        }

        private async void CalenderMeetingsGridViewSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                await
                    CommonHelper.GetInstance()
                        .CommonMeetingGridViewSelectionChanged(CalenderMeetingsGridView, _categoryService,
                            _navigationService, _globalCategoryList, _globalSubCategoryList, _globalCategoryId,
                            _globalSubCategoryId);
            }
            catch (Exception)
            {
            }
        }

        private async Task InitializeCalendar()
        {
            try
            {
                #region Ui List Generator

                _calenderGridWrappers.Add(Cal1);
                _calenderGridWrappers.Add(Cal2);
                _calenderGridWrappers.Add(Cal3);
                _calenderGridWrappers.Add(Cal4);
                _calenderGridWrappers.Add(Cal5);
                _calenderGridWrappers.Add(Cal6);
                _calenderGridWrappers.Add(Cal7);
                _calenderGridWrappers.Add(Cal8);
                _calenderGridWrappers.Add(Cal9);
                _calenderGridWrappers.Add(Cal10);
                _calenderGridWrappers.Add(Cal11);
                _calenderGridWrappers.Add(Cal12);

                _calenderGrids.Add(CalendarGrid1);
                _calenderGrids.Add(CalendarGrid2);
                _calenderGrids.Add(CalendarGrid3);
                _calenderGrids.Add(CalendarGrid4);
                _calenderGrids.Add(CalendarGrid5);
                _calenderGrids.Add(CalendarGrid6);
                _calenderGrids.Add(CalendarGrid7);
                _calenderGrids.Add(CalendarGrid8);
                _calenderGrids.Add(CalendarGrid9);
                _calenderGrids.Add(CalendarGrid10);
                _calenderGrids.Add(CalendarGrid11);
                _calenderGrids.Add(CalendarGrid12);

                _currentDateTexts.Add(CurrentDateText1);
                _currentDateTexts.Add(CurrentDateText2);
                _currentDateTexts.Add(CurrentDateText3);
                _currentDateTexts.Add(CurrentDateText4);
                _currentDateTexts.Add(CurrentDateText5);
                _currentDateTexts.Add(CurrentDateText6);
                _currentDateTexts.Add(CurrentDateText7);
                _currentDateTexts.Add(CurrentDateText8);
                _currentDateTexts.Add(CurrentDateText9);
                _currentDateTexts.Add(CurrentDateText10);
                _currentDateTexts.Add(CurrentDateText11);
                _currentDateTexts.Add(CurrentDateText12);

                #endregion

                var meetingsListByMonth = new List<List<DateTime>>();
                var datesListForMonth = new List<List<DateTime>>();
                _globalMeetings = await _meetingService.GetMeetings(_globalCategoryId, _globalSubCategoryId);

                const int sixMonths = 6;
                const int twelveMonths = 12;

                DateTime minDate;
                minDate = _globalMeetings.Count > 0 ? _globalMeetings.Min(x => x.Date).Date : DateTime.Now.Date;

                var diffNoOfMonths = ((minDate.Year - DateTime.Now.Year)*12) + minDate.Month - DateTime.Now.Month;
                if (diffNoOfMonths < 0)
                    diffNoOfMonths = diffNoOfMonths*-1;

                #region Previous months

                if (diffNoOfMonths >= sixMonths)
                {
                    //Display 6 months of past WITHOUT this month
                    if (_globalMeetings.Count > 0)
                    {
                        meetingsListByMonth.Add(
                            _globalMeetings.Where(
                                x =>
                                    x.Date.Month == DateTime.Now.AddMonths(-6).Month &&
                                    x.Date.Year == DateTime.Now.AddMonths(-6).Year).Select(y => y.Date).ToList());
                        meetingsListByMonth.Add(
                            _globalMeetings.Where(
                                x =>
                                    x.Date.Month == DateTime.Now.AddMonths(-5).Month &&
                                    x.Date.Year == DateTime.Now.AddMonths(-5).Year).Select(y => y.Date).ToList());
                        meetingsListByMonth.Add(
                            _globalMeetings.Where(
                                x =>
                                    x.Date.Month == DateTime.Now.AddMonths(-4).Month &&
                                    x.Date.Year == DateTime.Now.AddMonths(-4).Year).Select(y => y.Date).ToList());
                        meetingsListByMonth.Add(
                            _globalMeetings.Where(
                                x =>
                                    x.Date.Month == DateTime.Now.AddMonths(-3).Month &&
                                    x.Date.Year == DateTime.Now.AddMonths(-3).Year).Select(y => y.Date).ToList());
                        meetingsListByMonth.Add(
                            _globalMeetings.Where(
                                x =>
                                    x.Date.Month == DateTime.Now.AddMonths(-2).Month &&
                                    x.Date.Year == DateTime.Now.AddMonths(-2).Year).Select(y => y.Date).ToList());
                        meetingsListByMonth.Add(
                            _globalMeetings.Where(
                                x =>
                                    x.Date.Month == DateTime.Now.AddMonths(-1).Month &&
                                    x.Date.Year == DateTime.Now.AddMonths(-1).Year).Select(y => y.Date).ToList());
                    }

                    datesListForMonth.Add(new List<DateTime> {DateTime.Now.AddMonths(-6)});
                    datesListForMonth.Add(new List<DateTime> {DateTime.Now.AddMonths(-5)});
                    datesListForMonth.Add(new List<DateTime> {DateTime.Now.AddMonths(-4)});
                    datesListForMonth.Add(new List<DateTime> {DateTime.Now.AddMonths(-3)});
                    datesListForMonth.Add(new List<DateTime> {DateTime.Now.AddMonths(-2)});
                    datesListForMonth.Add(new List<DateTime> {DateTime.Now.AddMonths(-1)});
                }
                else
                {
                    for (var i = 1; i <= diffNoOfMonths; i++)
                    {
                        var mon = i*-1;
                        if (_globalMeetings.Count > 0)
                            meetingsListByMonth.Add(
                                _globalMeetings.Where(
                                    x =>
                                        x.Date.Month == DateTime.Now.AddMonths(mon).Month &&
                                        x.Date.Year == DateTime.Now.AddMonths(mon).Year).Select(y => y.Date).ToList());
                        else
                            meetingsListByMonth.Add(new List<DateTime>());
                        datesListForMonth.Add(new List<DateTime> {DateTime.Now.AddMonths(mon)});
                    }
                }

                #endregion

                #region Next months

                if (diffNoOfMonths >= sixMonths)
                {
                    //Display 5 months of next WITH this month
                    if (_globalMeetings.Count > 0)
                    {
                        meetingsListByMonth.Add(
                            _globalMeetings.Where(
                                x =>
                                    x.Date.Month == DateTime.Now.AddMonths(0).Month &&
                                    x.Date.Year == DateTime.Now.AddMonths(0).Year).Select(y => y.Date).ToList());
                        meetingsListByMonth.Add(
                            _globalMeetings.Where(
                                x =>
                                    x.Date.Month == DateTime.Now.AddMonths(1).Month &&
                                    x.Date.Year == DateTime.Now.AddMonths(1).Year).Select(y => y.Date).ToList());
                        meetingsListByMonth.Add(
                            _globalMeetings.Where(
                                x =>
                                    x.Date.Month == DateTime.Now.AddMonths(2).Month &&
                                    x.Date.Year == DateTime.Now.AddMonths(2).Year).Select(y => y.Date).ToList());
                        meetingsListByMonth.Add(
                            _globalMeetings.Where(
                                x =>
                                    x.Date.Month == DateTime.Now.AddMonths(3).Month &&
                                    x.Date.Year == DateTime.Now.AddMonths(3).Year).Select(y => y.Date).ToList());
                        meetingsListByMonth.Add(
                            _globalMeetings.Where(
                                x =>
                                    x.Date.Month == DateTime.Now.AddMonths(4).Month &&
                                    x.Date.Year == DateTime.Now.AddMonths(4).Year).Select(y => y.Date).ToList());
                        meetingsListByMonth.Add(
                            _globalMeetings.Where(
                                x =>
                                    x.Date.Month == DateTime.Now.AddMonths(5).Month &&
                                    x.Date.Year == DateTime.Now.AddMonths(5).Year).Select(y => y.Date).ToList());
                    }

                    datesListForMonth.Add(new List<DateTime> {DateTime.Now.AddMonths(0)});
                    datesListForMonth.Add(new List<DateTime> {DateTime.Now.AddMonths(1)});
                    datesListForMonth.Add(new List<DateTime> {DateTime.Now.AddMonths(2)});
                    datesListForMonth.Add(new List<DateTime> {DateTime.Now.AddMonths(3)});
                    datesListForMonth.Add(new List<DateTime> {DateTime.Now.AddMonths(4)});
                    datesListForMonth.Add(new List<DateTime> {DateTime.Now.AddMonths(5)});
                }
                else
                {
                    var diff = twelveMonths - diffNoOfMonths;
                    for (var i = 0; i < diff; i++)
                    {
                        if (_globalMeetings.Count > 0)
                            meetingsListByMonth.Add(
                                _globalMeetings.Where(
                                    x =>
                                        x.Date.Month == DateTime.Now.AddMonths(i).Month &&
                                        x.Date.Year == DateTime.Now.AddMonths(i).Year).Select(y => y.Date).ToList());
                        else
                            meetingsListByMonth.Add(new List<DateTime>());
                        datesListForMonth.Add(new List<DateTime> {DateTime.Now.AddMonths(i)});
                    }
                }

                #endregion

                var inc = 0;
                foreach (var datesList in datesListForMonth)
                {
                    //Clear the grid whenever the user load the first time or change the date
                    _calenderGrids[inc].RowDefinitions.Clear();
                    _calenderGrids[inc].Children.Clear();

                    //datesListForMonth

                    var currentDates = meetingsListByMonth[inc];
                    //this.CurrentDate = DateTime.Today;
                    SelectedDate = DateTime.Today;

                    InitializeHeader(_currentDateTexts[inc], datesListForMonth[inc][0]);
                    InitializeDayLabelBoxes(_calenderGrids[inc]);
                    InitializePreviousMonthBoxes(_calenderGrids[inc], datesListForMonth[inc][0]);
                    InitializeCurrentMonthBoxes(_calenderGrids[inc], _calenderGridWrappers[inc], datesListForMonth[inc][0],
                        currentDates);
                    InitializeNextMonthBoxes(_calenderGrids[inc], datesListForMonth[inc][0]);
                    InitializeThisMonthCalenderDateDisplay();

                    inc++;
                }

                if (GlobalSelectedCalender == -1)
                {
                    await GetMeetings(DateTime.Now);
                    return;
                }

                var dateName = _currentDateTexts[GlobalSelectedCalender];
                await GetMeetings((DateTime) dateName.Tag);
            }
            catch (Exception)
            {
            }
            finally
            {
                PgbPage.Visibility = Visibility.Collapsed;
            }
        }

        private void InitializeHeader(TextBlock txtBlock, DateTime CurrentDate)
        {
            try
            {
                var date = new DateTime(CurrentDate.Year, CurrentDate.Month, 1);
                txtBlock.Tag = date;
                txtBlock.Text = date.ToString("MMMM, yyyy");
            }
            catch (Exception)
            {
            }
        }

        private void InitializeDayLabelBoxes(Grid calenderGrid)
        {
            try
            {
                var column = 0;
                calenderGrid.RowDefinitions.Add(new RowDefinition {Height = GridLength.Auto});
                foreach (var day in Enum.GetValues(typeof (DayOfWeek)))
                {
                    //Runtime generate controls and setting style
                    var box = new Rectangle {Style = Application.Current.Resources["CalendarLabelBox"] as Style};
                    box.SetValue(Grid.RowProperty, 0);
                    box.SetValue(Grid.ColumnProperty, column);

                    var textBlock = new TextBlock
                    {
                        Style = Application.Current.Resources["CalendarLabel"] as Style,
                        Text = day.ToString().Substring(0, 3).ToUpper()
                    };

                    //Runtime setting the control Grid.Row and Grid.Column XAML property value
                    textBlock.SetValue(Grid.RowProperty, 0);
                    textBlock.SetValue(Grid.ColumnProperty, column);

                    //Adding the box and the textblock control to the Grid during runtime
                    calenderGrid.Children.Add(box);
                    calenderGrid.Children.Add(textBlock);

                    column++;
                }
            }
            catch (Exception)
            {
            }
        }

        private void InitializePreviousMonthBoxes(Grid calenderGrid, DateTime CurrentDate)
        {
            try
            {
                var previousMonthDate = CurrentDate.AddMonths(-1);
                var previousMonthDateIteration = new DateTime(previousMonthDate.Year, previousMonthDate.Month,
                    DateTime.DaysInMonth(previousMonthDate.Year, previousMonthDate.Month));
                calenderGrid.RowDefinitions.Add(new RowDefinition {Height = new GridLength(1, GridUnitType.Star)});

                for (var dayOfWeek = (int) previousMonthDateIteration.DayOfWeek; dayOfWeek >= 0; dayOfWeek--)
                {
                    var item = new CalendarItemUserControl(previousMonthDateIteration, "",
                        Application.Current.Resources["CalendarOtherMonthItemBox"] as Style);

                    item.SetValue(Grid.RowProperty, 1);
                    item.SetValue(Grid.ColumnProperty, dayOfWeek);

                    calenderGrid.Children.Add(item);

                    previousMonthDateIteration = previousMonthDateIteration.AddDays(-1);
                }
            }
            catch (Exception)
            {
            }
        }

        private void InitializeCurrentMonthBoxes(Grid calenderGrid, Grid calenderGridWrapper, DateTime currentmonth,
            List<DateTime> currentDates)
        {
            try
            {
                var daysList = currentDates.Select(x => x.Day).ToList();
                var currentDate = currentmonth;

                var row = 1;
                var maxDay = DateTime.DaysInMonth(currentDate.Year, currentDate.Month);

                for (var day = 1; day <= maxDay; day++)
                {
                    var dateIteration = new DateTime(currentDate.Year, currentDate.Month, day);
                    var dayOfWeek = (int) dateIteration.DayOfWeek;
                    CalendarItemUserControl item = null;

                    if (daysList.Contains(day))
                    {
                        item = new CalendarItemUserControl(dateIteration, day.ToString(),
                            Application.Current.Resources["CalendarMeetingDaysItemBox"] as Style);
                    }
                    else
                    {
                        item = new CalendarItemUserControl(dateIteration, day.ToString(),
                            Application.Current.Resources["CalendarItemBox"] as Style);
                    }

                    //highlight current month's today date
                    if (currentDate.Month == DateTime.Now.Month && SelectedDate.CompareTo(dateIteration) == 0)
                        item.GridStyle = Application.Current.Resources["CalendarSelectedItemBox"] as Style;

                    item.SetValue(Grid.RowProperty, row);
                    item.SetValue(Grid.ColumnProperty, dayOfWeek);

                    calenderGrid.Children.Add(item);

                    if (dayOfWeek == 6 && day != maxDay)
                    {
                        row++;
                        calenderGrid.RowDefinitions.Add(new RowDefinition
                        {
                            Height = new GridLength(1, GridUnitType.Star)
                        });
                    }
                }

                if (GlobalSelectedCalender == -1)
                {
                    if (currentDate.Year == DateTime.Now.Year && currentDate.Month == DateTime.Now.Month)
                    {
                        foreach (var grid in _calenderGridWrappers)
                        {
                            grid.Background = new SolidColorBrush(Color.FromArgb(255, 23, 129, 183));
                        }
                        calenderGridWrapper.Background = new SolidColorBrush(Colors.DarkOrange);
                    }
                }
                else
                {
                    var grid = _calenderGridWrappers[GlobalSelectedCalender];
                    grid.Background = new SolidColorBrush(Colors.DarkOrange);
                }
            }
            catch (Exception)
            {
            }
        }

        private void InitializeNextMonthBoxes(Grid calenderGrid, DateTime CurrentDate)
        {
            try
            {
                var nextMonthDate = CurrentDate.AddMonths(1);
                var nextMonthDateIteration = new DateTime(nextMonthDate.Year, nextMonthDate.Month, 1);

                var lastRow = calenderGrid.RowDefinitions.Count - 1;

                if (nextMonthDateIteration.DayOfWeek != DayOfWeek.Sunday)
                    for (var dayOfWeek = (int) nextMonthDateIteration.DayOfWeek; dayOfWeek < 7; dayOfWeek++)
                    {
                        var item = new CalendarItemUserControl(nextMonthDateIteration, "",
                            Application.Current.Resources["CalendarOtherMonthItemBox"] as Style);

                        item.SetValue(Grid.RowProperty, lastRow);
                        item.SetValue(Grid.ColumnProperty, dayOfWeek);

                        calenderGrid.Children.Add(item);

                        nextMonthDateIteration = nextMonthDateIteration.AddDays(1);
                    }
            }
            catch (Exception)
            {
            }
        }

        private void InitializeThisMonthCalenderDateDisplay()
        {
            //TODO: read datetime from server and put here
            //DayTextBlock1.Text = DateTime.Now.Day.ToString();
            //MonthTextBlock1.Text = " " + DateTime.Now.ToString("MMM");
            //YearTextBlock1.Text = " " + DateTime.Now.Year.ToString();
        }

        private async void CalGrid_Tapped(object sender, TappedRoutedEventArgs e)
        {
            try
            {
                var selectedGrid = sender as Grid;

                if (_calenderGridWrappers.IndexOf(selectedGrid, 0) == GlobalSelectedCalender) return;

                foreach (var grid in _calenderGridWrappers)
                {
                    grid.Background = new SolidColorBrush(Color.FromArgb(255, 23, 129, 183));
                }
                selectedGrid.Background = new SolidColorBrush(Colors.DarkOrange);

                var index = _calenderGridWrappers.IndexOf(selectedGrid, 0);
                GlobalSelectedCalender = index;
                var dateName = _currentDateTexts[index];
                await GetMeetings((DateTime) dateName.Tag);
            }
            catch (Exception)
            {
            }
            finally
            {
                if (Window.Current.Bounds.Width <= Utility.GetNarrowWidthSize())
                {
                    if (CalenderMeetingsGridView != null && CalenderMeetingsGridView.DataContext != null && 
                        ((List<MeetingModel>)(CalenderMeetingsGridView.DataContext)).Count > 0)
                    {
                        Grid.SetColumn(Grid2, 0);
                        Grid.SetColumnSpan(Grid2, 26);
                        Grid.SetRow(Grid2, 0);
                        Grid.SetRowSpan(Grid2, 2);
                        Grid2.Margin = new Thickness(3, 0, 0, 0);
                        Grid2.Visibility = Visibility.Visible;
                        Grid1.Visibility = Visibility.Collapsed;
                    }
                }
            }
        }

        private void NarrowBackButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Grid2.Visibility = Visibility.Collapsed;
                Grid1.Visibility = Visibility.Visible;
            }
            catch (Exception)
            {
            }
        }

        private async Task GetMeetings(DateTime currentmonth)
        {
            try
            {
                var meetings =
                    _globalMeetings.Where(x => x.Date.Month == currentmonth.Month && x.Date.Year == currentmonth.Year)
                        .ToList();

                if (meetings == null || meetings.Count <= 0)
                {
                    await
                        CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
                            () => { CalenderMeetingsGridView.DataContext = null; });
                    return;
                }

                meetings = await CommonHelper.GetInstance().CommonListViewUiManipulations(_authService, meetings);

                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    // Your UI update code goes here!
                    CalenderMeetingsGridView.DataContext = meetings;
                });
            }
            catch (Exception)
            {
            }
        }

        private void pageRoot_Unloaded(object sender, RoutedEventArgs e)
        {
            _navigationService = null;
            _categoryService = null;
            _meetingService = null;
        }

        
    }
}